package org.example.design.mediator.test1;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * @author huangshen
 * @version 0
 * @description 登录界面
 * @since 2022/2/11 10:22
 */
public class LoginFrame extends Frame implements Mediator, ActionListener {

    private ColleagueCheckBox checkGuest;
    private ColleagueCheckBox checkLogin;

    private ColleagueTextField textUser;
    private ColleagueTextField textPass;

    private ColleagueButton buttonOk;
    private ColleagueButton buttonCancel;

    public LoginFrame(String title) {
        super(title);
        setBackground(Color.lightGray);
        setSize(6000,4000);
        // 使用布局管理器生产4X2窗格
        setLayout(new GridLayout(4, 2));
        // 生成各个Colleague
        createColleague();
        // 配置
        add(checkGuest);
        add(checkLogin);
        add(new Label("UserName:"));
        add(textUser);
        add(new Label("Password:"));
        add(textPass);
        add(buttonOk);
        add(buttonCancel);
        // 设置初始启用/禁用状态
        colleagueChanged();
        // 显示
        pack();
        setVisible(true);
    }
    /**
     * 生成管理的组员
     */
    @Override
    public void createColleague() {
        // 生成
        CheckboxGroup group = new CheckboxGroup();
        checkGuest = new ColleagueCheckBox("Guest", group, true);
        checkLogin = new ColleagueCheckBox("Login", group, false);
        textUser = new ColleagueTextField("",10);
        textPass = new ColleagueTextField("",10);
        textPass.setEchoChar('*');
        buttonOk = new ColleagueButton("OK");
        buttonCancel = new ColleagueButton("Cancel");
        // 设置Mediator
        checkLogin.setMediator(this);
        checkGuest.setMediator(this);
        textUser.setMediator(this);
        textPass.setMediator(this);
        buttonOk.setMediator(this);
        buttonCancel.setMediator(this);
        // 设置Listener
        checkLogin.addItemListener(checkLogin);
        checkGuest.addItemListener(checkGuest);
        textUser.addTextListener(textUser);
        textPass.addTextListener(textPass);
        buttonOk.addActionListener(this);
        buttonCancel.addActionListener(this);
    }

    /**
     * 组员向仲裁者汇报
     */
    @Override
    public void colleagueChanged() {
        if(checkGuest.getState()) {
            textUser.setColleagueEnabled(false);
            textPass.setColleagueEnabled(false);
            buttonOk.setColleagueEnabled(true);
        } else {
            textUser.setColleagueEnabled(true);
            userPassChanged();
        }
    }

    /**
     * 当textUser或textPass文本输入框中的文字发生变化时
     * 判断各Colleague的启用/禁用状态
     */
    private void userPassChanged() {

        if(textUser.getText().length() > 0) {
            textPass.setColleagueEnabled(true);
            buttonOk.setColleagueEnabled(textPass.getText().length() > 0);
        } else {
            textPass.setColleagueEnabled(false);
            buttonOk.setColleagueEnabled(false);
        }
    }

    /**
     * Invoked when an action occurs.
     * @param e
     */
    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println(e.toString());
        System.exit(0);
    }
}
